﻿$(function () {
        var filterText = "";
        $("#tblMain tr").find("td:eq(2)").click(function () {
            $table = $("#tblMain")
            var clickedText = $.trim($(this).text());
            filterText = ((filterText == clickedText) ? "" : clickedText);
            $.uiTableFilter($table, filterText, '状态');
        });
            //点击链接弹出窗口
            $("input[type='button'].addUpdateCard").live("click", function () {
                cleanForm();
                $("div.popupWindow").show("slow");
                var btnCurrent = this;
                $('#cardlatestUpdateDate').datepicker();
                $.ajax({
                    url: "../../asset/getAllUsers",
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        $("#cardBelongsTo").empty();
                        $.each(data.employees, function (i, item) {
                            if (btnCurrent.name == "update") {
                            var $parent = $(btnCurrent).parent().parent();
                            var owner = $.trim($("td:eq(3)", $parent).text());
                            if (owner == item.alias) {
                                $("#cardBelongsTo").append("<option value='" + item.alias + "' selected='selected'>" + item.alias + "</option>");
                            } else {
                                $("#cardBelongsTo").append("<option value='" + item.alias + "'>" + item.alias + "</option>");
                            }
                        }
                        else {
                            $("#cardBelongsTo").append("<option value='" + item.alias + "'>" + item.alias + "</option>");
                        }
                      });
                    }
                });
        if (this.name == "update") {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "updateCardTitle").html("更新员工卡信息");
            $("input#cardNo").attr("readonly", true);
            var $parent = $(this).parent().parent();
            $("input#cardNo").val($.trim($("td:eq(1)", $parent).text()));
            $("input#cardstatus").val($.trim($("td:eq(2)", $parent).text()));
          //  $("input#cardbelongto").val($.trim($("td:eq(3)", $parent).text()));
            $("#cardlatestUpdateDate").val($.trim($("td:eq(4)", $parent).text()));

        } else {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "newAddCardTitle").html("添加新的卡信息");
            $("input#cardNo").removeAttr("readonly");
            $("input#cardNo").val("")
        }
        $("#cardNo").focus();
        //获取页面文档的高度
        var docheight = $(document).height();
        //追加一个层，使背景变灰
        $("#greybackground").css({ "opacity": "0.5", "height": docheight });
        $("#greybackground").show();
        return false;
    });
    //批量修改
    $("input[type='button'].batchUpdateCard").live("click", function () {
        var buttonCurrent = this;
        if ($("input[type='checkbox']:checked", $("#tblMain tr td")).length == 0) {
            alert("请选择需要修改的磁卡");
            return;
        }
        if (confirm("确定修改选中的" + $("input[type='checkbox']:checked", $("#tblMain tr td")).length + "项磁卡?")) {
            $("div.batchWindow").show("slow");
            $('#batchDate').datepicker();
            //获取页面文档的高度
            var docheight = $(document).height();
            //追加一个层，使背景变灰
            $("#greybackground").css({ "opacity": "0.5", "height": docheight });
            $("#greybackground").show();
            var ids = $("input[type='checkbox']:checked", $("#tblMain tr td"));
            $.each(ids, function (i, item) {
                if (i == 0) {
                    queryData = item.id;
                } else {
                    queryData += '||' + item.id;
                }
            });
        }
    });
    //批量提交
    $("input#batchSubmit").click(function () {

        if ($("#batchDate").val() == "") {
            $("span", $("#batchDate").parent()).text("日期不能为空！");
            return false;
        }
        requestUrl = "../../card/batchUpdate";
        $.ajax({
            url: requestUrl,
            type: "POST",
            data: { "ids": queryData, "latestUpdateDate": $("#batchDate").val(), "status": $("#batchStatus option:selected").val(), "cardNo": $("#cardNo").val(), "belongto": $("#cardBelongsTo").val()},
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 2:
                        $("div.batchWindow").hide();
                        $("#greybackground").hide();
                        alert("更新成功！");
                        window.location.reload();
                        break;
                    case 4:
                        break;
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });
    //提交表单
    $("input#btnSubmit").click(function () {
        if ($("input#cardNo").val() == "") {
            $("span", $("input#cardNo").parent()).text("cardNo 主键不能为空！");
            return false;
        }
        if ($("input#cardstatus").val() == "") {
            $("span", $("input#cardstatus").parent()).text("status状态不能为空！");
            return false;
        }
        var requestUrl = "../../card/submit";
        if ($("h2:eq(0)", $("div.popupWindow")).attr("id") == "updateCardTitle") {
            requestUrl = "../../card/update";
        }
        $.ajax({
            url: requestUrl,
            type: "POST",
            data: { "cardNo": $("input#cardNo").val(), "status": $("select#cardstatus").val(), "belongto": $("select#cardBelongsTo option:selected").text(), "latestUpdateDate": $("#cardlatestUpdateDate").val() },
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 1:
                        $("span", $("input#cardNo").parent()).text("该用户已有磁卡！");
                        break;
                    case 2:
                        $("div.popupWindow").hide();
                        $("#greybackground").hide();
                        alert("操作成功！");
                        window.location.reload();
                        break;
                    case 4:
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });
    //清空表单
    $("input#btnReset").click(function () {
        cleanForm();
    });
    //删除选中卡号
    $("input#btnDelete").click(function () {
        if ($("input[type='checkbox']:checked", $("#tblMain tr td")).length == 0) {
            alert("请选择需要删除的cardNo");
            return;
        }
        if (confirm("确定删除选中的" + $("input[type='checkbox']:checked", $("#tblMain tr td")).length + "项Card?")) {
            var queryData = "";
            var ids = $("input[type='checkbox']:checked", $("#tblMain tr td"));
            $.each(ids, function (i, item) {
                if (i == 0) {
                    queryData = item.id;
                } else {
                    queryData += '||' + item.id;
                }
            });
            $.ajax({
                url: "../../card/delete",
                type: "POST",
                data: { "ids": queryData },
                dataType: "json",
                success: function (data) {
                    switch (data.status) {
                        case 2:
                            alert("操作成功！");
                            window.location.reload();
                            break;
                        case 4:
                        default:
                            alert("出错啦，联系管理员！");
                            break;
                    }
                }
            });
        }
    });
    function cleanForm() {
        if ($("input#cardNo").attr("readonly") != true) $("input#cardNo").val("");
        $("input#cardstatus").val("");
        $("input#cardbelongto").val("");
        $("#cardlatestUpdateDate").val("");
        $("span", $("input#cardNo").parent()).text(" *");
        $("span", $("input#cardstatus").parent()).text(" *");
    }
});